package com.ay.wiki_plt.service.Impl;

import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.domain.Ebook;
import com.ay.wiki_plt.mapper.EbookMapper;
import com.ay.wiki_plt.service.EbookService;
import com.ay.wiki_plt.utils.SnowFlake;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EbookServiceImpl implements EbookService {

    @Autowired
    private EbookMapper ebookMapper;

    @Autowired
    private SnowFlake snowFlake;

    @Override
    public PageBean list(Integer page, Integer pageSize, String name,Long category2Id) {
        // 开启PageHelper插件
        PageHelper.startPage(page, pageSize);
        List<Ebook> ebookList = ebookMapper.list(name,category2Id);
        /*封装pagebean对象*/
        Page<Ebook> pageinfo = (Page<Ebook>) ebookList;
        return new PageBean(pageinfo.getTotal(), pageinfo.getResult());
    }

    /*电子书保存和修改接口*/
    @Override
    public void save(Ebook ebook) {
        Long ebookId = ebook.getId();
        if(ebookId ==null){
            //新增电子书
            ebook.setId(snowFlake.nextId());
            ebookMapper.saveEbook(ebook);
        } else{
            //修改电子书
            ebookMapper.updateEbook(ebook);
        }
    }

    /*电子书删除接口*/
    @Override
    public void delete(List<Long> ids) {
        ebookMapper.deleteByEbookList(ids);
    }


    /*根据id查询电子书*/
    @Override
    public Ebook listOne(Integer id) {
        return ebookMapper.getByIdEbook(id);
    }
}
